package com.fw.service.payatallinterface.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.outboundmessagelog.OutboundMessageLogService;
import com.fw.service.outboundmessagelog.bean.OutboundMessageLogBean;
import com.fw.service.payatallinterface.PayAtAllInterfaceService;
import com.fw.service.persistence.dao.TransactionDAO;
import com.fw.service.persistence.dto.Transaction;
import com.fw.service.property.PropertyService;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;
import com.fw.util.StringUtils;

public class PayAtAllInterfaceServiceImpl implements PayAtAllInterfaceService {

    @Autowired
    private TransactionDAO transactionDAO;

    @Autowired
    private OutboundMessageLogService outboundMessageLogService;

    @Autowired
    private PropertyService propertyService;

    @Override
    public OutboundMessageLogBean createOutboundMessageLog(InboundMessageLogBean inboundMessageLogBean,
	    ServiceProfileBean serviceProfileBean) {
	OutboundMessageLogBean returnOutboundMessageLogBean = null;
	try {
	    if (inboundMessageLogBean != null && serviceProfileBean != null) {
		String invoiceNo = StringUtils.getCashFormat(inboundMessageLogBean.getServiceID(), inboundMessageLogBean.getInvoiceNo());
		OutboundMessageLogBean outboundMessageLogBean = new OutboundMessageLogBean();
		outboundMessageLogBean.setMid("1234567890");
		outboundMessageLogBean.setServiceid("02");
		outboundMessageLogBean.setInv(invoiceNo);
		outboundMessageLogBean.setDesp(inboundMessageLogBean.getProductDesc());
		outboundMessageLogBean.setAmt(StringUtils.stringToBigDecimal(inboundMessageLogBean.getAmount()).toString());
		// payAtAllDTO.setCurr_type(ValidationImpl.CURRENCY_CODE_THB);
		// payAtAllDTO.setLanguage(ValidationImpl.LANGUAGE_TH);
		outboundMessageLogBean.setCurrency_code(inboundMessageLogBean.getCurrencyCode());
		outboundMessageLogBean.setLanguage(inboundMessageLogBean.getLanguage());
		outboundMessageLogBean.setCustomer_name(inboundMessageLogBean.getCustomerName());
		outboundMessageLogBean.setCustomer_email(inboundMessageLogBean.getCustomerEmail());
		outboundMessageLogBean.setCustomer_phone(inboundMessageLogBean.getCustomerPhone());

		// set from service config
		// if (Boolean.TRUE.equals(serviceProfileBean.getIsCancelUrl())) {
		// inboundMessageLogBean.setCancelURL(serviceProfileBean.getCancelUrl());
		// }
		// response back, cancel, confirm
		outboundMessageLogBean.setResp_url_back(propertyService
			.getProperty("com.fw.service.outboundmessagelog.bean.OutboundMessageLogBean.resp_url_back.cash"));
		outboundMessageLogBean.setResp_url_cancel(propertyService
			.getProperty("com.fw.service.outboundmessagelog.bean.OutboundMessageLogBean.resp_url_cancel.cash"));
		outboundMessageLogBean.setResp_url_confirm(propertyService
			.getProperty("com.fw.service.outboundmessagelog.bean.OutboundMessageLogBean.resp_url_confirm.cash"));

		returnOutboundMessageLogBean = outboundMessageLogService.createOutboundMessageLog(outboundMessageLogBean);

		// update OutboundMessageLogId
		if (inboundMessageLogBean != null && inboundMessageLogBean.getId() != null) {
		    Transaction transaction = transactionDAO.findByInboundMessageLogId(inboundMessageLogBean.getId());
		    if (transaction != null) {
			transaction.setOutboundMessageLogId(returnOutboundMessageLogBean.getId());
			transactionDAO.save(transaction);
		    }
		}

	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return returnOutboundMessageLogBean;
    }
}
